public class SingleLinkedListImpl implements SingleLinkedList {
	private class Item{
		String str;
		Item next;
	}
	private Item head;
	private int myMaxSize;
	
	void setMaxSize(int maxSize) {
		myMaxSize = maxSize;
	}
	
	public boolean isEmpty(){
		if (head == null) {
			return true;
		}
		else {
		return false;
		}
	}
	public  int size(){
		if (isEmpty() != true) {
			int i = 1;
			Item item = head;
			while (item.next != null) {
				i++;
				item = item.next;
			}
			return i;
		}
		else {
			return 0;
		}
	}
	public  void insertFirst(String value){
		checkSize();
		if (head == null){
			Item item = new Item();
			item.str = value;
			item.next = null;
			head = item;
		}
		else{
			Item item = new Item();
			item.str = value;
			item.next = head;
			head = item;
		}
		
	}
	public void insertLast(String value){
		checkSize();
		if (head != null) {
			Item item = head;
			while (item.next != null) {
				item = item.next;
			}
			Item item2 = new Item();
			item2.str = value;
			item.next = item2;
			item2.next = null;
		}
		else {
			insertFirst(value);
			}
	}
	public void insert(String value, int position){
		checkSize();
		int s = size();
		if (position == s) {
			insertLast(value);
			return;
		}
		checkBounds(position);
		if (position == 0) {
			insertFirst(value);
		}
		if ((position < s) && (position > 0)) {
			int i = 0;
			Item item = head;
			while (i < position - 1) {
				item = item.next;
				i++;
			}
			Item item2 = new Item();
			item2.str = value;
			item2.next = item.next;
			item.next = item2;
		}
	}
	
	private void checkSize() {
		System.err.println("size="+size()+" maxsize="+myMaxSize);
		if (size()<myMaxSize) {
			return;
		}
		throw new IllegalStateException("My size is too big and="+size());
	}

	public String get(int position){
		int i = 0;	
		if (head != null) {
		  Item item = head;
		  while ((item.next != null) && (i!=position)) { 
			item = item.next;
			i++;
		  }
		  return item.str;
		  }
		else {
		    return null;
			}
		
	}

	public void remove(int position){
		checkBounds(position);
		Item item = head;
		int i=0;
				if (position == 0) {
				head = head.next;
				}
					else{
					while (i != position -1) {
						item = item.next;
						i++;
					}
					item.next=item.next.next;
					}
	}
	
	void checkBounds(int position) {
		int s = size();
		if (position < 0) {
			throw new IndexOutOfBoundsException("privet");
		}
		if (position >= s) {
			throw new IndexOutOfBoundsException("wrong index="+position);
		}
	}
	public void clear(){
		head = null;
	}
	public void swap(int pos1, int pos2){
		checkBounds(pos1);
		checkBounds(pos2);
	}
	
	
	
}